<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ], function(array, xhr,
            request, topic, JSON) {
        var deletItemsCount = 0;
        workplanRestForm = function(formId) {
            dijit.byId(formId).reset();
            if (formId.indexOf('UFD') != -1) {
                var rowid = jQuery("#workplanGird").jqGrid('getGridParam', 'selrow');
                var workplan = jQuery("#workplanGird").jqGrid('getRowData', rowid);
                dijit.byId('workplanUFD').setValues(workplan);
            }
        };
        topic.subscribe("workplan/insertOrUpdate", workplanRestForm);
        var syncDB = function(url, jsonData) {
            request.post(url, {
                data : jsonData,
                headers : {
                    'Content-Type' : "application/json; charset=utf-8"
                },
                timeout : 30000
            }).then(function(response) {
                topic.publish("notifyMsgTopic", {
                    message : response,
                    type : "message",
                    duration : 1000
                });
                var currentPage = jQuery('#workplanGird').jqGrid('getGridParam', 'page');
                var pageSize = jQuery('#workplanGird').jqGrid('getGridParam', 'rowNum');
                var totalCount = jQuery('#workplanGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                var totalPages = Math.ceil(totalCount / pageSize);
                jQuery("#workplanGird").jqGrid('setGridParam', {
                    page : currentPage > totalPages ? totalPages : currentPage
                }).trigger("reloadGrid");
            }, function(error) {
                alert(error);
            });
        };
        workplanSubmitForm = function(formId, type) {
            var url = "workplan/insert";
            if (type == 'u') {
                url = "workplan/update";
            }
            var form = dijit.byId(formId);
            if (form.isValid()) {
                var json = JSON.stringify(form.getValues());
                syncDB(url, json);
                form.hide();
                topic.publish("workplan/insertOrUpdate", formId);
            } else {
                form.validate();
            }
        };
        workplanDelete = function() {
            var ids = jQuery("#workplanGird").jqGrid('getGridParam', 'selarrrow') || [];
            deletItemsCount = ids.length;
            if (ids.length > 0) {
                if (confirm('确认删除所选定的项？')) {
                    var workplan = {};
                    workplan.id = '';
                    array.forEach(ids, function(entry, i) {
                        workplan.id += entry + ",";
                    });
                    syncDB("workplan/delete", JSON.stringify(workplan));
                }
            } else {
                alert('请选择至少一条数据!');
            }
        };
        workplanQuery = function(formId) {
            var workplan = dijit.byId(formId).getValues();
            jQuery("#workplanGird").jqGrid('setGridParam', {
                postData : workplan,
                mtype : "post"
            }).trigger('reloadGrid');
        };
    });
</script>
<div data-dojo-type="dijit/Dialog" id="workplanIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="planname">计划名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="planname"></td>
      </tr>
      <tr>
        <td><label for="plancontent">计划内容: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="plancontent"></td>
      </tr>
      <tr>
        <td><label for="starttime">开始日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="starttime"></td>
      </tr>
      <tr>
        <td><label for="endtime">结束日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="endtime"></td>
      </tr>
      <tr>
        <td><label for="typename">类型名称（个人计划为空）: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="typename"></td>
      </tr>
      <tr>
        <td><label for="userid">用户ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="userid"></td>
      </tr>
      <tr>
        <td><label for="protypeid">计划类别ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="protypeid"></td>
      </tr>
      <tr>
        <td><label for="issuescope">发布范围：0则代表全部部门，存放所有的参与部门ID（个人计划为空）: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="issuescope"></td>
      </tr>
      <tr>
        <td><label for="participants">参与人：0则代表全部参与，参与人,即员工ID列表（个人计划为空）: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="participants"></td>
      </tr>
      <tr>
        <td><label for="principal">负责人: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="principal"></td>
      </tr>
      <tr>
        <td><label for="note">备注: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="note"></td>
      </tr>
      <tr>
        <td><label for="status">状态：1=激活，0=禁用: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="status"></td>
      </tr>
      <tr>
        <td><label for="ispersonal">是否为个人计划：1=则为个人工作计划，这时发布范围，参与人均为空，负责人为当前用户，0=则代表为其他任务（部门计划）: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="ispersonal"></td>
      </tr>
      <tr>
        <td><label for="icon">图标: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="icon"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="workplanSubmitForm('workplanIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="workplanRestForm('workplanIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="workplanUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text" name="planid" style="display: none;">
    <table>
      <tr>
        <td><label for="planname">计划名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="planname"></td>
      </tr>
      <tr>
        <td><label for="plancontent">计划内容: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="plancontent"></td>
      </tr>
      <tr>
        <td><label for="starttime">开始日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="starttime"></td>
      </tr>
      <tr>
        <td><label for="endtime">结束日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="endtime"></td>
      </tr>
      <tr>
        <td><label for="typename">类型名称（个人计划为空）: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="typename"></td>
      </tr>
      <tr>
        <td><label for="userid">用户ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="userid"></td>
      </tr>
      <tr>
        <td><label for="protypeid">计划类别ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="protypeid"></td>
      </tr>
      <tr>
        <td><label for="issuescope">发布范围：0则代表全部部门，存放所有的参与部门ID（个人计划为空）: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="issuescope"></td>
      </tr>
      <tr>
        <td><label for="participants">参与人：0则代表全部参与，参与人,即员工ID列表（个人计划为空）: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="participants"></td>
      </tr>
      <tr>
        <td><label for="principal">负责人: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="principal"></td>
      </tr>
      <tr>
        <td><label for="note">备注: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="note"></td>
      </tr>
      <tr>
        <td><label for="status">状态：1=激活，0=禁用: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="status"></td>
      </tr>
      <tr>
        <td><label for="ispersonal">是否为个人计划：1=则为个人工作计划，这时发布范围，参与人均为空，负责人为当前用户，0=则代表为其他任务（部门计划）: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="ispersonal"></td>
      </tr>
      <tr>
        <td><label for="icon">图标: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="icon"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="workplanSubmitForm('workplanUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="workplanRestForm('workplanUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryWorkplanForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td><label for="planid">计划ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="planid"></td>
        </tr>
        <tr>
          <td><label for="planname">计划名称: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="planname"></td>
        </tr>
        <tr>
          <td><label for="plancontent">计划内容: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="plancontent"></td>
        </tr>
        <tr>
          <td><label for="starttime">开始日期: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="starttime"></td>
        </tr>
        <tr>
          <td><label for="endtime">结束日期: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="endtime"></td>
        </tr>
        <tr>
          <td><label for="typename">类型名称（个人计划为空）: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="typename"></td>
        </tr>
        <tr>
          <td><label for="userid">用户ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="userid"></td>
        </tr>
        <tr>
          <td><label for="protypeid">计划类别ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="protypeid"></td>
        </tr>
        <tr>
          <td><label for="issuescope">发布范围：0则代表全部部门，存放所有的参与部门ID（个人计划为空）: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="issuescope"></td>
        </tr>
        <tr>
          <td><label for="participants">参与人：0则代表全部参与，参与人,即员工ID列表（个人计划为空）: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="participants"></td>
        </tr>
        <tr>
          <td><label for="principal">负责人: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="principal"></td>
        </tr>
        <tr>
          <td><label for="note">备注: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="note"></td>
        </tr>
        <tr>
          <td><label for="status">状态：1=激活，0=禁用: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="status"></td>
        </tr>
        <tr>
          <td><label for="ispersonal">是否为个人计划：1=则为个人工作计划，这时发布范围，参与人均为空，负责人为当前用户，0=则代表为其他任务（部门计划）: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="ispersonal"></td>
        </tr>
        <tr>
          <td><label for="icon">图标: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="icon"></td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="workplanInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('workplanIFD').show();
      </script>
    </button>
    <button id="workplanDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="workplanDelete();"></button>
    <button id="workplanUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#workplanGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var workplan = jQuery("#workplanGird").jqGrid('getRowData', ids[0]);
          dijit.byId('workplanUFD').setValues(workplan);
          dijit.byId('workplanUFD').show();
        }
      </script>
    </button>
    <button id="workplanQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询" onclick="workplanQuery('queryWorkplanForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="workplanGird"></table>
  <div id="workplanToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#workplanGird").jqGrid(
            {
                url : 'workplan/queryAsList',
                datatype : "json",
                autowidth : true,
                rownumbers : true,
                height : 'auto',
                colNames : [ '计划ID', '计划名称', '计划内容', '开始日期', '结束日期', '类型名称（个人计划为空）', '用户ID', '计划类别ID',
                        '发布范围：0则代表全部部门，存放所有的参与部门ID（个人计划为空）', '参与人：0则代表全部参与，参与人,即员工ID列表（个人计划为空）', '负责人', '备注',
                        '状态：1=激活，0=禁用', '是否为个人计划：1=则为个人工作计划，这时发布范围，参与人均为空，负责人为当前用户，0=则代表为其他任务（部门计划）', '图标' ],
                colModel : [ {
                    name : 'planid',
                    index : 'planid'
                }, {
                    name : 'planname',
                    index : 'planname'
                }, {
                    name : 'plancontent',
                    index : 'plancontent'
                }, {
                    name : 'starttime',
                    index : 'starttime'
                }, {
                    name : 'endtime',
                    index : 'endtime'
                }, {
                    name : 'typename',
                    index : 'typename'
                }, {
                    name : 'userid',
                    index : 'userid'
                }, {
                    name : 'protypeid',
                    index : 'protypeid'
                }, {
                    name : 'issuescope',
                    index : 'issuescope'
                }, {
                    name : 'participants',
                    index : 'participants'
                }, {
                    name : 'principal',
                    index : 'principal'
                }, {
                    name : 'note',
                    index : 'note'
                }, {
                    name : 'status',
                    index : 'status'
                }, {
                    name : 'ispersonal',
                    index : 'ispersonal'
                }, {
                    name : 'icon',
                    index : 'icon'
                } ],
                rowNum : 15,
                rowList : [ 15, 20, 25, 30 ],
                pager : '#workplanToolbar',
                // sortname : 'username',
                recordpos : 'right',
                viewrecords : true,
                sortorder : "desc",
                jsonReader : {
                    repeatitems : false
                },
                multiselect : true,
                shrinkToFit : false,
                caption : "工作计划列表"
            });
    jQuery("#workplanGird").jqGrid('navGrid', '#workplanToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
